<template>
	<view class="container">
		<h3 class="title"><span>{{dataJson.share_user.nickname}}</span>送你一份礼物请查收</h3>
		<div class="txtMain">
			{{dataJson.content}}
		</div>
		<view class="gifImg">
			<image :src="item" mode="heightFix" v-for="(item,index) in  dataJson.imgs" :key="index"></image>
		</view>
		<!-- <view class="list">
			<view class="items" v-for="(item, index) in dataJson.info" :key="item.id">
				<image class="item-image" :src="item.thumb" mode="aspectFill"></image>
				<div class="r">
					<h3 class="title">{{item.title}}</h3>
					<view class="box">
						<p class="desc">数量:{{item.num}}</p>
					</view>
				</div>
			</view>
		</view> -->
		<button class="shareBtn" @tap="getReceive()"><text class="button-text">确认接收</text></button>
	</view>
</template>

<script>
	import {
		request
	} from '@/utils/request.js'
	export default {
		data() {
			return {
				dataJson: {},
				category: 'people',
			}
		},
		onLoad(options) {
			console.log(options)
			if (options.order_sn) {
				this.getInfo(options.order_sn)
			}
			
			// #ifdef MP-WEIXIN
			wx.hideShareMenu(); // 隐藏右上角分享按钮
			// #endif
		},
		computed: {},
		onShareAppMessage() {
			return {
				title: '送你一份大礼', // 分享标题
				path: '', // 分享路径，默认当前页面路径
				imageUrl: '/static/images/shop1.jpg', // 分享图片
				success(res) {
					console.log('分享成功', res);
				},
				fail(err) {
					console.log('分享失败', err);
				}
			}
		},
		methods: {
			getInfo(order_sn) {
				
				request({
					url: 'user/share-info',
					method: 'get',
					data: {
						order_sn: order_sn
					}
				}).then(res => {
					console.log(res)
					if (res.status == 200) {
						this.dataJson = res.data

					}
				})
			},
			getReceive() {
				const userInfo = uni.getStorageSync('userInfo');
				if(userInfo.uid==this.dataJson.share_user.id){
					uni.showToast({
						title: '您不可以领取自己赠送的礼物哦',
						icon: 'none'
					})
					return
				}
				request({
					url: 'user/receive',
					method: 'post',
					data: {
						order_sn: this.dataJson.order_num
					}
				}).then(res => {
					if (res.status == 200) {
						uni.showToast({
							title: '领取成功前往仓库查看',
							icon: 'none'
						})
						setTimeout(()=>{
							uni.switchTab({
								url:"/pages/cart/cart"
							})
						},2000)
					}else{
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				})
			},
			checkLogin() {
			  const token = uni.getStorageSync('token')
			  if (!token) {
			    // 未登录，跳转到登录页或直接登录
			    this.wxLogin()
			  } else {
			    // 已登录，验证token有效性
			    // this.verifyToken(token)
			  }
			},
			// 微信登录
			wxLogin() {
			  uni.login({
			    provider: 'weixin',
			    success: (loginRes) => {
			      if (loginRes.code) {
			        // 获取到临时登录凭证，发送到后端
			        this.code2Session(loginRes.code)
			      } else {
			        console.error('登录失败！' + loginRes.errMsg)
			        uni.showToast({
			          title: '登录失败，请重试',
			          icon: 'none'
			        })
			      }
			    },
			    fail: (err) => {
			      console.error('uni.login失败:', err)
			      uni.showModal({
			        title: '提示',
			        content: '无法获取微信登录权限，请检查设置',
			        showCancel: false
			      })
			    }
			  })
			},
			
			// 发送code到后端换取token
			code2Session(code) {
				let params={
					js_code:code
				}
			  request({
			    url: 'login-wechat/info',
			    method: 'get',
			    data: params
			  }).then(res => {
			    if (res.status === 200 && res.data.token) {
			      // 登录成功，保存token
			      uni.setStorageSync('token', res.data.token)
			      uni.setStorageSync('userInfo', res.data)
			      
			      // 触发登录成功事件，通知其他页面
			      uni.$emit('loginSuccess', res.data)
			    } else {
			      console.error('登录接口错误:', res.msg)
			      uni.showToast({
			        title: res.msg || '登录失败',
			        icon: 'none'
			      })
			    }
			  }).catch(err => {
			    console.error('登录请求失败:', err)
			    uni.showToast({
			      title: '网络请求失败，请重试',
			      icon: 'none'
			    })
			  })
			},

		}
	}
</script>

<style lang="scss">
	.container {
		padding: 20rpx;
	}

	.input-area {
		align-items: center;
		margin-bottom: 20rpx;
		position: relative;

		textarea {
			border: 1px solid #eee;
			width: 100%;
			height: 300rpx;
			border-radius: 10rpx;
			padding: 15rpx;
			font-size: 28rpx;
			box-sizing: border-box;
		}

		.emojiBtn {
			position: absolute;
			right: 0;
			bottom: 0;
			z-index: 9;
		}
	}

	.input-area input {
		flex: 1;
		margin: 0 10rpx;
		padding: 10rpx;
		border: 1rpx solid #eee;
		border-radius: 5rpx;
	}

	.emoji-panel {
		background: #fff;
		border-radius: 10rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.emoji-tabs {
		display: flex;
		border-bottom: 1rpx solid #eee;
		padding: 10rpx;
	}

	.emoji-tabs text {
		padding: 10rpx 20rpx;
		margin-right: 10rpx;
		border-radius: 5rpx;
	}

	.emoji-tabs text.active {
		background: #f0f0f0;
	}

	.emoji-content {
		display: flex;
		flex-wrap: wrap;
		padding: 10rpx;
	}

	.emoji-content text {
		font-size: 28rpx;
		padding: 10rpx;
		width: 60rpx;
		text-align: center;
	}

	.message-list view {
		padding: 10rpx 0;
		border-bottom: 1rpx solid #eee;
	}

	.title {
		text-align: center;
		font-size: 32rpx;
		margin-bottom: 30rpx;

		span {
			color: red;
		}
	}

	.gifList {
		display: flex;
		flex-wrap: wrap;

		.li {
			width: 180rpx;
			height: 180rpx;
			padding: 10rpx;
			box-sizing: border-box;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}

	.gifImg {
		image {
			height: 300rpx;
			display: block;
			margin: 0 auto;
		}
	}

	.list {
		margin-top: 30rpx;

		.items {
			box-sizing: border-box;
			box-shadow: 0 0 14px rgba(0, 0, 0, .1);
			height: 258rpx;
			padding: 12rpx;
			margin-bottom: 30rpx;
			border-radius: 10rpx;
			display: flex;
			align-items: center;
			position: relative;

			.box {
				display: flex;
				align-items: center;
				justify-content: space-between;
			}

			image {
				height: 228rpx;
				width: 228rpx;
				margin-right: 20rpx;
			}

			.add {
				background: url(/static/images/add.png) no-repeat;
				background-size: 100% 100%;
				width: 50rpx;
				height: 50rpx;
				display: block;
				position: absolute;
				bottom: 12rpx;
				right: 12rpx;
			}

			.r {
				width: 200rpx;
				height: 100%;
				padding: 20rpx 0;
				box-sizing: border-box;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				flex: 1;

				.title {
					font-size: 32rpx;
					text-align: left;
				}

				.desc {
					font-size: 28rpx;
					color: #666;
				}

				.price {
					color: #4a90e2;
					font-size: 25rpx;

					span {
						font-size: 32rpx;
					}
				}
			}
		}
	}

	.shareBtn {
		width: 300rpx;
		background-color: #e74c3c;
		color: #fff;
	}

	.pickerSlide {
		position: absolute;
		left: 0;
		bottom: 0;
		width: 200rpx;
		height: 60rpx;
		line-height: 60rpx;
		z-index: 9;
		font-size: 28rpx;
		text-align: center;
		color: #666;
		padding: 0 20rpx;
		background-color: #f8f8f8;
		;
		border: 1px solid #eee;
		border-top-right-radius: 10rpx;
	}

	.item-count {
		display: flex;
		align-items: center;
	}

	.count-btn {
		width: 50rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		border: 1rpx solid #ddd;
		font-size: 32rpx;
	}

	.count-num {
		width: 70rpx;
		text-align: center;
		font-size: 28rpx;
	}

	.txtMain {
		font-size: 28rpx;
		color: #666;
		margin: 40rpx 0;
	}
</style>